
#region Apache Notice
/*****************************************************************************
 * $Header: $
 * $Revision: 591621 $
 * $Date: 2007-11-03 07:44:57 -0600 (Sat, 03 Nov 2007) $
 * 
 * iBATIS.NET Data Mapper
 * Copyright (C) 2004 - Gilles Bayon
 *  
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 ********************************************************************************/
#endregion

#region Using

using System;
using System.Collections;
using System.Collections.Specialized;
using System.Data;
using System.Data.SqlClient;
using System.Reflection;
using System.Text;
using SX.Utils.Log.log4net;
using IBatisNet.Common.Utilities;
using IBatisNet.Common.Utilities.Objects;
using IBatisNet.DataMapper.Configuration.ParameterMapping;
using IBatisNet.DataMapper.Exceptions;
using IBatisNet.DataMapper.Scope;

#endregion

namespace IBatisNet.DataMapper.Configuration.Statements
{
	/// <summary>
	/// Summary description for PreparedStatementFactory.
	/// </summary>
	public class PreparedStatementFactory
	{

		#region Fields

		private PreparedStatement _preparedStatement = null;

		private string _parameterPrefix = string.Empty;
		private IStatement _statement = null;
		//private ISqlMapSession _session = null;
		private string _commandText = string.Empty;
		private RequestScope _request = null;
		// (property, DbParameter)
		private HybridDictionary _propertyDbParameterMap = new HybridDictionary();

		private static readonly ILog _logger = LogManager.GetLogger( MethodBase.GetCurrentMethod().DeclaringType );

		#endregion

		/// <summary>
		/// Constructor
		/// </summary>
		/// <param name="session"></param>
		/// <param name="statement"></param>
		/// <param name="commandText"></param>
		/// <param name="request"></param>
		public PreparedStatementFactory(ISqlMapSession session, RequestScope request, IStatement statement, string commandText)
		{
			//_session = session;
			_request = request;
			_statement = statement;
			_commandText = commandText;
		}


		/// <summary>
		/// Create a list of IDataParameter for the statement and build the sql string.
		/// </summary>
		public PreparedStatement Prepare()
		{
			_preparedStatement = new PreparedStatement();
			_parameterPrefix = "@";

			_preparedStatement.PreparedSql = _commandText;

			 

			if (_logger.IsDebugEnabled) 
			{
				_logger.Debug("Statement Id: [" + _statement.Id + "] Prepared SQL: [" + _preparedStatement.PreparedSql + "]");
			}

			return _preparedStatement;
		}
         
	}
}
